<?php

/**
 * PageController is the default controller for this application
 */
class PageController extends ControllerBase {
	public $pageCategoryDataService;
	public $pageDataService;
	
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'page' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if ($authentication->isAdministrator () == false) {
			
			// Go to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of page category
		$this->smarty->assign ( 'statusList', PageEntity::getStatusList () );
		
		$this->pageDataService = new PageDataService ();
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site. Return list of all page category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View page_category list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of page_category
	 * landing page of the site. Return list of all page_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'pageEntities', $this->pageDataService->getItems ( null ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Page/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add action is trigger when admin want to add the page_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {
		
		$isError = false;
		$error = "";
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->validateActionKey ()) 

		{
			
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// get content
			$content = $this->getPost ( 'content', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$content = stripslashes ( $content );
			}
			
			$content = trim ( $content );
			
			// head title
			$headTitle = $this->getPost ( 'headTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Get status field
			$status = $this->getPost ( 'status', 0 );
			
			// check
			if (! empty ( $name ) && ! empty ( $content ) && 

			is_numeric ( $status )) {
				$status = intval ( $status );
				
				$authentication = new Authentication ();
				try {
					// Save data into database
					$pageEntity = $this->pageDataService->beforeCreate ();
					$pageEntity->name = $name;
					$pageEntity->content = $content;
					$pageEntity->headTitle = $headTitle;
					$pageEntity->status = $status;
					$pageEntity->metaDescription = $metaDescription;
					$pageEntity->metaKeywords = $metaKeywords;
					$pageEntity->ownerId = $authentication->getCurrentUserId ();
					
					$pageId = $this->pageDataService->create ( $pageEntity );
					
					// Redirect to edit page
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Page/edit/id/' . $pageId . '/justCreated/1/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		// Create new token key
		$this->createActionKey ();
		
		$this->smarty->assign ( 'msg', $error );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'name', isset ( $name ) ? $name : "" );
		$this->smarty->assign ( 'content', isset ( $content ) ? $content : "" );
		$this->smarty->assign ( 'headTitle', isset ( $headTitle ) ? $headTitle : "" );
		$this->smarty->assign ( 'status', isset ( $status ) ? $status : 0 );
		$this->smarty->assign ( 'metaDescription', isset ( $metaDescription ) ? $metaDescription : "" );
		$this->smarty->assign ( 'metaKeywords', isset ( $metaKeywords ) ? $metaKeywords : "" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Page/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit action is trigger when admin want to edit the page_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		// Error flag
		$isError = false;
		$error = "";
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of page does not exist, go on to view list page
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->listAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// just created
		$justCreated = $this->getQuery ( 'justCreated' );
		if (! empty ( $justCreated ) && $justCreated == "1") {
			$error = "The item has been created successfully";
		}
		
		// Get data
		$pageEntity = $this->pageDataService->getById ( $id );
		
		// check data
		if (empty ( $pageEntity )) {
			$this->listAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->validateActionKey ()) {
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// get content
			$content = $this->getPost ( 'content', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$content = stripslashes ( $content );
			}
			
			$content = trim ( $content );
			$content = htmlentities ( $content, ENT_QUOTES );
			
			// head title
			$headTitle = $this->getPost ( 'headTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Get status field
			$status = $this->getPost ( 'status', 0 );
			
			$pageEntity->name = $name;
			$pageEntity->content = $content;
			$pageEntity->headTitle = $headTitle;
			$pageEntity->status = $status;
			$pageEntity->metaDescription = $metaDescription;
			$pageEntity->metaKeywords = $metaKeywords;
			
			// check
			if (! empty ( $name ) && ! empty ( $content ) && is_numeric ( $status )) {
				try {
					$res = $this->pageDataService->update ( $pageEntity );
					
					$isError = false;
					
					// Show a message
					$error = 'The record has been updated successfully';
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message				
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'id', $pageEntity->id );
		$this->smarty->assign ( 'name', $pageEntity->name );
		$this->smarty->assign ( 'content', $pageEntity->content );
		$this->smarty->assign ( 'headTitle', $pageEntity->headTitle );
		$this->smarty->assign ( 'status', $pageEntity->status );
		$this->smarty->assign ( 'metaDescription', $pageEntity->metaDescription );
		$this->smarty->assign ( 'metaKeywords', $pageEntity->metaKeywords );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Page/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a page_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get Id of page
		$id = $this->getPost ( 'idValue', 0 );
		
		if ($this->validateActionKey () && ! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			// Delete page
			$res = $this->pageDataService->del ( $id );
		}
		// View page_category list
		$this->listAction ();
	}

}
